//
//  ZYComposeToolBar.swift
//  weiboZY
//
//  Created by 赵勇 on 16/5/19.
//  Copyright © 2016年 赵勇. All rights reserved.
//

import UIKit

enum ZYComposeToolBarButtonType: Int {
    
    case Picture = 0
    case Mention = 1
    // 话题
    case Trend = 2
    // 表情
    case Emoticon = 3
    //add
    case Add  = 4
    
    
}

class ZYComposeToolBar: UIStackView {
    
    // 设置点击按钮的闭包回调
    var didSeletedButtonClosure:((type: ZYComposeToolBarButtonType)->())?
    

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        // 指定布局
        axis = .Horizontal
        // 填充方式
        distribution = .FillEqually
        
        addChildButton("compose_toolbar_picture", type: .Picture)
        addChildButton("compose_mentionbutton_background", type: .Mention)
        addChildButton("compose_trendbutton_background", type: .Trend)
        addChildButton("compose_emoticonbutton_background", type: .Emoticon)
        addChildButton("compose_add_background", type: .Add)
        
    }
    
    // 添加子控件按钮
    private func addChildButton(imageName:String, type: ZYComposeToolBarButtonType) {
        
        let btn = UIButton()
        // 获取枚举原始值
        btn.tag = type.rawValue
        btn.addTarget(self, action: "btnClick:", forControlEvents: .TouchUpInside)
        btn.setImage(UIImage(named: imageName), forState: .Normal)
        btn.setImage(UIImage(named: "\(imageName)_highlighted"), forState: .Highlighted)
        
        btn.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: .Normal)
        btn.adjustsImageWhenHighlighted = false
        addArrangedSubview(btn)
    }
    
    @objc private func btnClick(button: UIButton) {
      
        let type = ZYComposeToolBarButtonType(rawValue: button.tag)!
        
        didSeletedButtonClosure?(type: type)
        
    }

    
}
